package com.foxnicweb.app.patent.service.bpm;

import com.foxnicweb.app.constants.enums.patent.PatentProcessStatus;
import com.foxnicweb.app.domain.patent.meta.PatentProcessMeta;
import org.github.foxnic.web.domain.bpm.BpmActionResult;
import org.github.foxnic.web.domain.bpm.BpmEvent;
import org.github.foxnic.web.framework.bpm.BpmEventAdaptor;
import org.github.foxnic.web.framework.bpm.BpmAssistant;
import javax.annotation.Resource;

import org.github.foxnic.web.proxy.bpm.ProcessInstanceRemindServiceProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.foxnic.commons.collection.MapUtil;
import java.util.Arrays;


import com.foxnicweb.app.domain.patent.PatentProcess;
import com.foxnicweb.app.patent.service.IPatentProcessService;

/**
 * <p>
 * 审批流程实例流程回调事件适配器
 * </p>
 * @author 李方捷 , leefangjie@qq.com
 * @since 2023-04-18 10:29:20
*/

public class PatentProcessBpmEventAdaptor extends BpmEventAdaptor<PatentProcess,IPatentProcessService> {

	public PatentProcessBpmEventAdaptor(IPatentProcessService service) {
		super(service);
	}

	/***
	 * 流程暂存开始，通过返回 BpmActionResult  的 success 或  failure 控制暂存过程是否继续进行
	 * */
	protected BpmActionResult onTemporarySaveStart(BpmEvent event) {
		return event.getActionResult();
	}


	/***
	 * 流程提交/启动开始，通过返回 BpmActionResult  的 success 或  failure 控制流程提交/启动过程是否继续进行
	 * */
	protected BpmActionResult onProcessSubmitStart(BpmEvent event) {
		return event.getActionResult();
	}

	/***
	 * 流程待办开始，通过返回 BpmActionResult  的 success 或  failure 控制流程待办处理过程是否继续进行
	 * */
	protected BpmActionResult onTaskStart(BpmEvent event) {

		BpmActionResult result=event.getActionResult();
		PatentProcess  bill =  this.getFormData(event);
		if(bill==null) {
			result.success(false).message("缺少业务单据").messageLevel4Confirm();
			return result;
		}

		String nodeId = event.getCurrentNode().getCamundaNodeId();
		if("N1".equals(nodeId)) {
			if (bill.getIsGotBase() == 1) {
				this.service().join(bill, PatentProcessMeta.CASE_AGENT_LIST);
				if (bill.getCaseAgentList() == null || bill.getCaseAgentList().isEmpty()) {
					result.success(false).message("请指定代理人").messageLevel4Confirm();
					return result;
				}
			} else {
				this.service().join(bill, PatentProcessMeta.OUT_PERSON_LIST);
				if (bill.getOutPersonList() == null || bill.getOutPersonList().isEmpty()) {
					result.success(false).message("请指定外协人员").messageLevel4Confirm();
					return result;
				}
			}
		} else if("N15".equals(nodeId)) {
			this.service().join(bill, PatentProcessMeta.BUSINESS_ASSISTANT_LIST);
			if (bill.getBusinessAssistantList() == null || bill.getBusinessAssistantList().isEmpty()) {
				result.success(false).message("请指定商务助理").messageLevel4Confirm();
				return result;
			}
		}

 		// 指定流程变量
		result.putVariable("is_got_base", bill.getIsGotBase());
		result.putVariable("no_base_continue", bill.getNoBaseContinue());
		result.putVariable("can_office_done", bill.getCanOfficeDone());
		result.putVariable("is_material_complete", bill.getIsMaterialComplete());
		result.putVariable("can_agent_done", bill.getCanAgentDone());
		result.putVariable("finalized", bill.getFinalized());
		result.putVariable("retrieval", bill.getRetrieval());
		return result;
	}

	/***
	 * 流程节点执行前，此事件由 camunda 提供，返回值仅在 act_ru_event_callback 表记录，不做其它控制
	 * */
	protected BpmActionResult onNodeStart(BpmEvent event) {

		BpmActionResult result=event.getActionResult();
		PatentProcess  bill =  this.getFormData(event);

		String nodeId = event.getCurrentNode().getCamundaNodeId();
		if(nodeId.equals("N4")) {  //N4:代理人主管派案给代理人
			// 已检索派案
			bill.setStatusEnum(PatentProcessStatus.B01);
		} else if(nodeId.equals("N2")) {
			bill.setStatusEnum(PatentProcessStatus.B04);
		} else if(nodeId.equals("N12")) {
			bill.setStatusEnum(PatentProcessStatus.B13);
		} else if(nodeId.equals("N10")) {
			bill.setStatusEnum(PatentProcessStatus.B15);
		} else if(nodeId.equals("N14")) {
			bill.setStatusEnum(PatentProcessStatus.C01);
		} else if(nodeId.equals("N16")) {
			bill.setStatusEnum(PatentProcessStatus.C07);
		} else if(nodeId.equals("N19")) {
			bill.setStatusEnum(PatentProcessStatus.C08);
		} else if(nodeId.equals("N20")) {
			bill.setStatusEnum(PatentProcessStatus.D01);
		}
		service().updateDirtyFields(bill);
		return result;
	}

	/***
	 * 流程节点执行后，此事件由 camunda 提供，返回值仅在 act_ru_event_callback 表记录，不做其它控制
	 * */
	protected BpmActionResult onNodeEnd(BpmEvent event) {

		BpmActionResult result=event.getActionResult();
		PatentProcess  bill =  this.getFormData(event);

		String nodeId = event.getCurrentNode().getCamundaNodeId();
		if(nodeId.equals("N6")) {
			// 已派案,待检索
			bill.setStatusEnum(PatentProcessStatus.B07);
		} else if(nodeId.equals("N7")) {
			bill.setStatusEnum(PatentProcessStatus.B10);
		} else if(nodeId.equals("N11")) {
			bill.setStatusEnum(PatentProcessStatus.Z01);
		} else if(nodeId.equals("N13")) {
			bill.setStatusEnum(PatentProcessStatus.Z01);
		} else if(nodeId.equals("N15")) {
			bill.setStatusEnum(PatentProcessStatus.C04);
		} else if(nodeId.equals("N21")) {
			bill.setStatusEnum(PatentProcessStatus.D04);
		} else if(nodeId.equals("N3")) {
			if(bill.getNoBaseContinue()==0) {
				bill.setStatusEnum(PatentProcessStatus.Z01);
			}
		}
		service().updateDirtyFields(bill);
		return result;
	}

	/***
	 * 流程撤回开始，通过返回 BpmActionResult  的 success 或  failure 控制流程撤回处理过程是否继续进行
	 * */
	protected BpmActionResult onFetchBackStart(BpmEvent event) {
		return event.getActionResult();
	}

	/***
	 * 流程撤回结束，返回值无意义
	 * */
	protected BpmActionResult onFetchBackEnd(BpmEvent event) {
		return event.getActionResult();
	}

	/***
	 * 流程跳转开始，通过返回 BpmActionResult  的 success 或  failure 控制流程跳转处理过程是否继续进行
	 * */
	protected BpmActionResult onJumpStart(BpmEvent event) {
		return event.getActionResult();
	}

	/***
	 * 流程跳转结束， 返回值无意义
	 * */
	protected BpmActionResult onJumpEnd(BpmEvent event) {
		return event.getActionResult();
	}

	/***
	 * 流程转办开始，通过返回 BpmActionResult  的 success 或  failure 控制流程转办处理过程是否继续进行
	 * */
	protected BpmActionResult onDelegateStart(BpmEvent event) {
		return event.getActionResult();
	}

	/***
	 * 流程转办处理结束，返回值无意义
	 * */
	protected BpmActionResult onDelegateEnd(BpmEvent event) {
		return event.getActionResult();
	}

	/***
	 * 流程废弃开始，通过返回 BpmActionResult  的 success 或  failure 控制流程废弃处理过程是否继续进行
	 * */
	protected BpmActionResult onProcessAbandonStart(BpmEvent event) {
		return event.getActionResult();
	}

	/***
	 * 流程废弃结束，返回值无意义
	 * */
	protected BpmActionResult onProcessAbandonEnd(BpmEvent event) {
		return event.getActionResult();
	}

}
